<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/23
 * Time: 11:46
 */

namespace Admin\Controller;


use Common\Controller\AuthController;
use OT\TagLib\Think;

class VerifyController extends AuthController {

    public function verify_list(){

        $obj = D('B2c');
        $p = I('p')?I('p'):1;
        $condition['state'] = 1;
        $count = $obj->table('xf_total')->where($condition)->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = $obj->table('xf_total')->where($condition)->order('id desc')->page($p,10)->select();
        foreach ($list as $k=>$v){

            $comid = $v['comid'];
            $u_name = $obj->table('tb_company')->where(array('ComID'=>$comid))->field('name')->find();

            $list[$k]['u_name'] = $u_name['name'];

        }

        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->display();

    }

    public function verify_table(){

        $id = I('id');
        $obj = D('B2c');
        $data = $obj->table('xf_total')->where(array('id'=>$id))->find();
        $u_name = $obj->table('tb_company')->where(array('ComID'=>$data['comid']))->field('name')->find();
        $res = json_decode(strval(trim($data['finnal_info'])),true);
        $oldjson = strval(trim($data['finnal_info']));
        //先进类别
        $xj_type = [0=>['name'=>'高性能纤维及复合材料'],1=>['name'=>'新型金属功能材料'],2=>['name'=>'新型能源材料'],3=>['name'=>'绿色建筑材料(建筑工业化部品部件)'],4=>['name'=>'3D打印材料(增材制造)']];
        //智能类别
        $zn_type = [0=>['name'=>'新能源装置装备'],1=>['name'=>'新型商用车'],2=>['name'=>'节能环保装备'],3=>['name'=>'智能终端'],4=>['name'=>'大数据']];
        $city = M('city_list')->select();
        $this->assign('zn_type',$zn_type);
        $this->assign('xj_type',$xj_type);
        $country = M('country_list')->select();
        $goods = M('goods_catgory')->select();
        $this->assign('city',$city);
        $this->assign('country',$country);
        $this->assign('goods',$goods);
        $this->assign('hphp',$oldjson);
        $this->assign('res',$res);
        $this->assign('info',$data);
        $this->assign('u_name',$u_name['name']);
        $this->display();

    }

    public function verify_table_run(){

        if (!IS_AJAX){
            $this->error('提交方式不正确',0,0);
        }
        $id = I('tableid');
        $obj = D('B2c');
        $state = I('verify');
        if ($state == 3){
            $olddata = $obj->table('xf_total')->where(array('id'=>$id))->field('finnal_info')->find();
            $oldarr = json_decode(strval(trim($olddata['finnal_info'])),true);

            $newlast = I('last');
            $newnow = I('now');
            //foreach 定义自增变量
            $i = 0;
            //对相应的数据进行更新
            foreach ($oldarr as $key=>$value){
                foreach ($value as $k=>$v){

                    if ($k == 'last'){
                        $oldarr[$key][$k] = $newlast[$i];
                    }elseif ($k == 'now'){
                        $oldarr[$key][$k] = $newnow[$i];
                    }

                }
                $i++;


            }
            $newstr = json_encode($oldarr);
            $res = $obj->table('xf_total')->where(array('id'=>$id))->setField(array('finnal_info'=>$newstr,'state'=>3));
            if ($res){
                $this->success('修改成功',U('verify_list'),0);
            }
        }elseif ($state == 2){
            $olddata = $obj->table('xf_total')->where(array('id'=>$id))->field('finnal_info')->find();
            $oldarr = json_decode(strval(trim($olddata['finnal_info'])),true);

            $newlast = I('last');
            $newnow = I('now');
            //foreach 定义自增变量
            $i = 0;
            //对相应的数据进行更新
            foreach ($oldarr as $key=>$value){
                foreach ($value as $k=>$v){

                    if ($k == 'last' && $oldarr[$key][$k]!=$newlast[$i]){
                        $oldarr[$key][$k] = $newlast[$i];
                        $oldarr[$key]['last_update'] = 'true';
                    }elseif ($k == 'now' && $oldarr[$key][$k] != $newnow[$i]){
                        $oldarr[$key][$k] = $newnow[$i];
                        $oldarr[$key]['now_update'] = 'true';
                    }

                }
                $i++;


            }
            $newstr = json_encode($oldarr);
            $res = $obj->table('xf_total')->where(array('id'=>$id))->setField(array('finnal_info'=>$newstr,'state'=>2));
            if ($res){
                $this->success('修改成功',U('verify_list'),0);
            }
        }

    }

    public function tg_list(){
        $obj = D('B2c');
        $p = I('p')?I('p'):1;
        $condition['state'] = 3;
        $count = $obj->table('xf_total')->where($condition)->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = $obj->table('xf_total')->where($condition)->order('id desc')->page($p,10)->select();
        foreach ($list as $k=>$v){

            $comid = $v['comid'];
            $u_name = $obj->table('tb_company')->where(array('ComID'=>$comid))->field('name')->find();

            $list[$k]['u_name'] = $u_name['name'];

        }

        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->display();

    }

    public function table_info(){

        $id = I('id');
        $obj = D('B2c');
        $data = $obj->table('xf_total')->where(array('id'=>$id))->find();
        $u_name = $obj->table('tb_company')->where(array('ComID'=>$data['comid']))->field('name')->find();
        $res = json_decode(trim($data['finnal_info']),true);
        $this->assign('u_name',$u_name['name']);
        $this->assign('res',$res);
        $this->assign('info',$data);
        $this->display();

    }

    public function bh_list(){

        $obj = D('B2c');
        $p = I('p')?I('p'):1;
        $condition['state'] = 2;
        $count = $obj->table('xf_total')->where($condition)->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = $obj->table('xf_total')->where($condition)->order('id desc')->page($p,10)->select();
        foreach ($list as $k=>$v){

            $comid = $v['comid'];
            $u_name = $obj->table('tb_company')->where(array('ComID'=>$comid))->field('name')->find();

            $list[$k]['u_name'] = $u_name['name'];

        }

        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->display();

    }

    public function test(){

        $obj = D('B2c');
        $con2['year'] = 2017;
        $con2['quarter'] = 1;
        $con2['code'] ='CDTLG417';
        $con1['year'] = 2017;
        $con1['quarter'] = 1;
        $con1['code'] ='CDTLG416';
        $res1 = $obj->table('xf_total')->where($con1)->order('code asc')->select();
        $res2 = $obj->table('xf_total')->where($con2)->order('code asc')->select();
        $arr1 = array();
        $arr2 = array();

        foreach ($res1 as $k=>$v){
            array_push($arr1,json_decode($v['finnal_info'],true));

        }
        //16表集合结果
        $arr = array();
        foreach ($arr1 as $key=>$value){
            foreach ($value as $k=>$v){
                foreach ($v as $aa=>$ss){
                    if ($aa=='last' && $ss!='#'){
                        if (is_numeric($ss)){
                            $arr[$k][$aa] += $ss;
                        }else{
                            $arr[$k][$aa] .= $ss.'、';
                        }

                    }
                    if ($aa=='now' && $ss!='#'){
                        if (is_numeric($ss)){
                            $arr[$k][$aa] += $ss;
                        }else{
                            $arr[$k][$aa] .= $ss.'、';
                        }
                    }

                }

            }

        }
        //17表集合
        $array = array();

        foreach ($res2 as $k=>$v){
            array_push($arr2,json_decode($v['finnal_info'],true));
        }
        foreach ($arr2 as $aa=>$bb){

            foreach ($bb as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k=='last' && $v!='#'){
                        if (is_numeric($v)){
                            $array[$key][$k] += $v;
                        }else{
                            $array[$key][$k] .= $v.'、';
                        }

                    }
                    if ($k=='now' && $v!='#'){
                        if (is_numeric($v)){
                            $array[$key][$k] += $v;
                        }else{
                            $array[$key][$k] .= $v.'、';
                        }
                    }
                }
            }

        }

        var_dump($arr);
        var_dump($array);

    }




}